//
//  PopoverPresentationController.swift
//  weiboFirst
//
//  Created by ZhouRobert on 15/6/30.
//  Copyright © 2015年 ZhouRobert. All rights reserved.
//

import UIKit

/// ios 8.0 推出的，专门用于负责视图控制器之间的转场
class PopoverPresentationController: UIPresentationController {

    
    //展现视图的大小
    var presentFrame = CGRectZero
    
    //1. 设置遮罩视图
    lazy var dummyView: UIView = {
        
    let v = UIView()
    
    v.backgroundColor = UIColor(white: 0.0, alpha: 0.2)
        
//      2. 监听点击事件
    let tap = UITapGestureRecognizer(target: self, action: "close")
        v.addGestureRecognizer(tap)

    return v
    }()
    /**
    containerView 容器视图，放置展现出来的视图的
    presentedView() 被展现出来的视图的
    
    */
    
    //3. 关闭展现视图的控制器
    func close() {
        presentedViewController.dismissViewControllerAnimated(true, completion: nil)
    }
    
    
///  4. 容器视图将要重新布局子视图
    
    override func containerViewWillLayoutSubviews() {
        super.containerViewWillLayoutSubviews()
        
        //添加并且设置dummyView
        dummyView.frame = containerView!.bounds
        containerView?.insertSubview(dummyView, atIndex: 0)
        
        //设置视图大小,老师的原始值是100，改为60，使这个弹窗在正中间
        presentedView()?.frame = presentFrame
    }

}
